package com.yh.jandan.ext.thread;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Db;
import com.yh.jandan.model.Meizi;
import com.yh.jandan.util.DateUtils;

//2020/1/24 18:02:06
public class MeiziCheckThread {
	static ConcurrentLinkedQueue < Meizi > mzs = new ConcurrentLinkedQueue < Meizi > ( );
	
	static boolean isRuning = false;
	
	public static void hold ( Meizi meizi ) {
		mzs.add ( meizi );
	}
	
	public static int getCurrentLength ( ) {
		return mzs.size ( );
	}
	
	public static void stop ( ) {
		isRuning = false;
	}
	
	public static void start ( ) {
		
		if ( isRuning ) return;
		isRuning = true;
		
		init ( );
		
		new Thread ( new Runnable ( ) {
			public void run ( ) {
				while ( isRuning ) {
					sleep_seconds ( 1 + System.currentTimeMillis ( ) % 4 );
					if ( mzs.isEmpty ( ) ) continue;
					Meizi meizi = mzs.poll ( );
					if ( null == meizi ) continue;
					boolean b = check_ok ( meizi.getBig ( ) );
					if ( b ) continue; // img ok
					hide ( meizi ); // hide error meizi img
				}
			}
		} ).start ( );
		
	}
	
	protected static void hide ( Meizi meizi ) {
		meizi.set ( "show" , "false" );
		Db.update ( "update meizi set `show`=? where md5=?" , "false" , meizi.get ( "md5" ) );
		write_log ( "update():[" + new Date ( ) + "]" + JsonKit.toJson ( meizi ) );
	}
	
	protected static boolean check_ok ( String img_src ) {
		HttpURLConnection urlcon = null;
		String src = img_src.startsWith ( "//" ) ? "http:" + img_src : img_src;
		// String src = img_src.startsWith ( "htt" ) ? img_src : "http:" + img_src;
		try {
			URL url = new URL ( src );
			urlcon = (HttpURLConnection) url.openConnection ( );
			urlcon.setUseCaches ( false );
			urlcon.setConnectTimeout ( 3 * 1000 );
			urlcon.setReadTimeout ( 3 * 1000 );
			urlcon.setRequestProperty ( "Content-type" , "application/x-www-form-urlencoded" );
			return urlcon.getResponseCode ( ) == HttpURLConnection.HTTP_OK;
		} catch ( Exception e ) {
			// e.printStackTrace ( );
		} finally {
			if ( null != urlcon ) {
				urlcon.disconnect ( );
				urlcon = null;
			}
		}
		return false;
	}
	
	private static void init ( ) {
		if ( null != mzs ) return;
		mzs = new ConcurrentLinkedQueue < Meizi > ( );
	}
	
	private static void sleep_seconds ( long i ) {
		try {
			TimeUnit.SECONDS.sleep ( i );
		} catch ( InterruptedException e ) {
			e.printStackTrace ( );
		}
		
	}
	
	private static void write_log ( String string ) {
		try {
			System.out.println ( string );
			FileUtils.write ( new File ( getLogFile ( ) ) , string + "\n" , true );
		} catch ( IOException e ) {
			e.printStackTrace ( );
		}
	}
	
	private static String getLogFile ( ) {
		String datestr = DateUtils.getDateByFormat ( DateUtils.YMD_FORMAT );
		return log_file.replace ( "{date}" , datestr );
	}
	
	static String log_file = "logs/MeiziCheckThread-{date}.log";
	
}
